package cn.exrick.xboot.modules.backlogOrFull.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.backlogOrFull.entity.BacklogOrFull;
import cn.exrick.xboot.modules.backlogOrFull.service.IBacklogOrFullService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "待办与已办管理接口")
@RequestMapping("/xboot/backlogOrFull")
@Transactional
public class BacklogOrFullController {

    @Autowired
    private IBacklogOrFullService iBacklogOrFullService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BacklogOrFull> get(@PathVariable String id){

        BacklogOrFull backlogOrFull = iBacklogOrFullService.getById(id);
        return new ResultUtil<BacklogOrFull>().setData(backlogOrFull);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<BacklogOrFull>> getAll(){
        /**
         * njp modify 20190812
         * 在获取全部数据的同时增加排序值ASC（升序排序）
         * begin
         */
         /*  List<BacklogOrFull> list = iBacklogOrFullService.list();*/
        List<BacklogOrFull> list  =iBacklogOrFullService.selectAllBacklogOrFull();
        /**
         * njp modify 20190812
         * 在获取全部数据的同时增加排序值ASC（升序排序）
         * end
         */
        return new ResultUtil<List<BacklogOrFull>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<BacklogOrFull>> getByPage(@ModelAttribute PageVo page){

        IPage<BacklogOrFull> data = iBacklogOrFullService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<BacklogOrFull>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<BacklogOrFull> saveOrUpdate(@ModelAttribute BacklogOrFull backlogOrFull){
        backlogOrFull.setUpdateTime(new Date());
        if(iBacklogOrFullService.saveOrUpdate(backlogOrFull)){
            return new ResultUtil<BacklogOrFull>().setData(backlogOrFull);
        }
        return new ResultUtil<BacklogOrFull>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iBacklogOrFullService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

}
